package org.huangxq.bigscreen.init

import com.alibaba.fastjson2.JSONArray
import jakarta.annotation.Resource
import org.huangxq.bigscreen.data.*
import org.huangxq.bigscreen.repository.ComplexIndConfigRepository
import org.huangxq.bigscreen.repository.SimpleIndConfigRepository
import org.huangxq.bigscreen.repository.ThemeNameRepository
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component

@Component
class DataInitializer: ApplicationRunner {

    @Resource
    lateinit var themeNameRepository: ThemeNameRepository
    @Resource
    lateinit var simpleIndConfigRepository: SimpleIndConfigRepository
    @Resource
    lateinit var complexIndConfigRepository: ComplexIndConfigRepository

    override fun run(args: ApplicationArguments?) {
        try {
            themeNameRepository.findById("report01").get()
        }catch (e: Exception){
            themeNameRepository.save(ThemeNameData("report01", "医院大屏驾驶仓"))
        }
        try {
            simpleIndConfigRepository.findById(SimpleIndConfigId("report01", "ind1")).get()
        }catch (e: Exception){
            simpleIndConfigRepository.save(SimpleIndConfigData(SimpleIndConfigId("report01", "ind1"), "门急诊人次"))
        }
        try {
            simpleIndConfigRepository.findById(SimpleIndConfigId("report01", "ind2")).get()
        }catch (e: Exception){
            simpleIndConfigRepository.save(SimpleIndConfigData(SimpleIndConfigId("report01", "ind2"), "待诊人数"))
        }
        try {
            simpleIndConfigRepository.findById(SimpleIndConfigId("report01", "ind3")).get()
        }catch (e: Exception){
            simpleIndConfigRepository.save(SimpleIndConfigData(SimpleIndConfigId("report01", "ind3"), "在院人数"))
        }
        try {
            simpleIndConfigRepository.findById(SimpleIndConfigId("report01", "ind4")).get()
        }catch (e: Exception){
            simpleIndConfigRepository.save(SimpleIndConfigData(SimpleIndConfigId("report01", "ind4"), "入院人次"))
        }
        try {
            simpleIndConfigRepository.findById(SimpleIndConfigId("report01", "ind5")).get()
        }catch (e: Exception){
            simpleIndConfigRepository.save(SimpleIndConfigData(SimpleIndConfigId("report01", "ind5"), "出院人次"))
        }
        try {
            simpleIndConfigRepository.findById(SimpleIndConfigId("report01", "ind6")).get()
        }catch (e: Exception){
            simpleIndConfigRepository.save(SimpleIndConfigData(SimpleIndConfigId("report01", "ind6"), "手术人次"))
        }
        try {
            complexIndConfigRepository.findById(ComplexIndConfigId("report01", "ind1")).get()
        }catch (e: Exception){
            val data = ComplexIndConfigData(ComplexIndConfigId("report01", "ind1"), "门急诊人次")
            data.sqlConfig = JSONArray().apply {
                add(SqlConfig("门诊人次", ""))
                add(SqlConfig("急诊人次", ""))
            }.toJSONString()
            complexIndConfigRepository.save(data)
        }
        try {
            complexIndConfigRepository.findById(ComplexIndConfigId("report01", "ind2")).get()
        }catch (e: Exception){
            val data = ComplexIndConfigData(ComplexIndConfigId("report01", "ind2"), "待诊人数")
            data.sqlConfig = JSONArray().apply {
                add(SqlConfig("门诊人次", ""))
                add(SqlConfig("急诊人次", ""))
            }.toJSONString()
            complexIndConfigRepository.save(data)
        }
        try {
            complexIndConfigRepository.findById(ComplexIndConfigId("report01", "ind3")).get()
        }catch (e: Exception){
            val data = ComplexIndConfigData(ComplexIndConfigId("report01", "ind3"), "出院人次")
            data.sqlConfig = JSONArray().apply {
                add(SqlConfig("出院人次", ""))
            }.toJSONString()
            complexIndConfigRepository.save(data)
        }
        try {
            complexIndConfigRepository.findById(ComplexIndConfigId("report01", "ind4")).get()
        }catch (e: Exception){
            val data = ComplexIndConfigData(ComplexIndConfigId("report01", "ind4"), "手术人次")
            data.sqlConfig = JSONArray().apply {
                add(SqlConfig("手术人次", ""))
            }.toJSONString()
            complexIndConfigRepository.save(data)
        }
        try {
            complexIndConfigRepository.findById(ComplexIndConfigId("report01", "map")).get()
        }catch (e: Exception){
            val data = ComplexIndConfigData(ComplexIndConfigId("report01", "map"), "本月住院情况")
            data.sqlConfig = JSONArray().apply {
                add(SqlConfig("入院", ""))
                add(SqlConfig("出院", ""))
            }.toJSONString()
            complexIndConfigRepository.save(data)
        }
    }
}